import pygame

SCREEN_SIZE = (480, 700)

class BaseSprite(pygame.sprite.Sprite):

    def __init__(self, filename, speed):
        super().__init__()
        # 把背景图加载进来
        self.image = pygame.image.load(filename)
        # 获取image的矩形区域
        self.rect = self.image.get_rect()
        self.speed = speed

    def update(self, *args):
        self.rect.y +=self.speed


class PlaneSprite(BaseSprite):
    def __init__(self, is_pre=False, speed = 1):
        super().__init__('./images/me1.png', speed)
        self.rect.centerx = int(SCREEN_SIZE[0]/2)
        self.rect.centery = SCREEN_SIZE[1]-100


class BackGroundSprite(BaseSprite):
    def __init__(self, is_pre=False, speed = 1):
        super().__init__('./images/background.png', speed)
        # 是否预备图
        x = 10
        if is_pre:
            # 设置预备图的起点
            self.rect.y = -SCREEN_SIZE[1]

    def update(self):

        super().update()
        # 超过坐标后改变坐标位置
        if self.rect.y>=SCREEN_SIZE[1]:
            self.rect.y = -SCREEN_SIZE[1]


class PlaneGame:
    """飞机大战游戏"""
    def __init__(self):
        # pygame 初始化
        pygame.init()
        # 设置窗口大小
        self.screen = pygame.display.set_mode(SCREEN_SIZE)
        # 设置窗口标题
        pygame.display.set_caption('Palne Game')
        # 获取时钟
        self.clock = pygame.time.Clock()
        # 是否循环
        self.is_loop = True
        self.__create_sprites()


    def start(self):
        while self.is_loop:
            self.__event_handler()
            # 填充颜色
            self.screen.fill((0,0,0))
            self.__update_sprites()
            # 每秒最多60帧
            self.clock.tick(60)
            # 界面更新
            pygame.display.update()
        #退出
        pygame.quit()

    def __create_sprites(self):
        """创建BackGroundSprite的对象"""
        bg = BackGroundSprite()
        bg1 = BackGroundSprite(True)
        self.bg_group = pygame.sprite.Group(bg, bg1)
        plane = PlaneSprite()
        self.plane_group = pygame.sprite.Group(plane)

    def __update_sprites(self):
        self.bg_group.update()
        self.bg_group.draw(self.screen)
        self.plane_group.draw(self.screen)

    def __event_handler(self):
        """事件处理"""
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                self.is_loop = False

pg = PlaneGame()
pg.screen
pg.start()